﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DoAn
{
    public class Object : VisibleGameEntity
    {
        private int _flag;//Phe ta = 1, NPC = 0;
        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }

        private int numberofStates;
        public int NumberofStates
        {
            get { return numberofStates; }
            set { numberofStates = value; }
        }

        public Object()
        {
            mySprites = new List<MySprite>();
            nSprites = 0;
            AutoRangeInit = new List<MySprite>();
        }

        private int _maxblood;
        public int Maxblood
        {
            get { return _maxblood; }
            set { _maxblood = value; }
        }


        private int _armor;
        public int Armor
        {
            get { return _armor; }
            set { _armor = value; }
        }

        private int _damage;
        public int Damage
        {
            get { return _damage; }
            set { _damage = value; }
        }

        static public bool Collision(MySprite a, MySprite b)
        {
            if ((a.ActualPos.X + a.ActualSize.X >= b.ActualPos.X) && (a.ActualPos.X <= b.ActualPos.X + b.ActualSize.X)
                && (a.ActualPos.Y + a.ActualSize.Y >= b.ActualPos.Y) && (a.ActualPos.Y <= b.ActualPos.Y + b.ActualSize.Y))
                return true;
            else
                return false;
        }

        public void TreatCollision(Vector2[,] a, bool[,] b, int w, int h, int c, int r)
        {
            int j; int i;
            for (i = 0; i < nSprites - 1; i++)
            {
                if (mySprites[i].Life)
                {
                    for (j = i + 1; j < nSprites; j++)
                    {
                        if (Collision(mySprites[i], mySprites[j]) && mySprites[j].Life)// && mySprites[j].Type < 6)
                        {
                            subTreatCollision(i, j, a, b, w, h, c, r);
                            subTreatCollision(j, i, a, b, w, h, c, r);
                            if (!Collision(mySprites[i], mySprites[j]))
                            {
                                mySprites[i].CreateDistanceGo();
                                mySprites[j].CreateDistanceGo();
                            }
                        }
                    }
                    //for (int d = 0; d < 12; d++)
                    //{
                    //    if(Collision(mySprites[i], Game1.DiaHinh[d]))
                    //        mySprites[i].avoidSprite(Game1.DiaHinh[d]);
                    //}
                }
            }
        }
        public Vector2 vr;
        private void subTreatCollision(int i, int j, Vector2[,] a, bool[,] b, int w, int h, int c, int r)
        {
            if (mySprites[i].DistanceGo != new Vector2(0))//nếu đang chạy
            {
                //if (mySprites[i].TypeMySprite < 6)
                //{
                    //float dis = (mySprites[i].Target - mySprites[i].Position).Length();//khoảng cách giữa position & target
                    //if (dis > mySprites[i].Size.X*1.5f)
                    //if (Game1.TestVector(mySprites[i].Target + new Vector2(mySprites[i].Size.X / 2, mySprites[i].Size.Y / 2)) &&
                    //    dis < mySprites[i].Size.X * 2f)
                    //{
                    //    //mySprites[i].avoidSprite(mySprites[j]);
                    //    //mySprites[i].CreateDistanceGo();//để định hướng lại đường đi sau khi gán DistanceGo = new Vector2(3, -1);
                    //    //if (dis < mySprites[i].Size.X)
                    //    Vector2 v = Game1.LocalCell(mySprites[i].Target);
                    //    //mySprites[i].Position -= (mySprites[i].DistanceGo) * 3;
                    //    //mySprites[i].Target = mySprites[i].Position + Size / 2;

                    //    othercell((int)v.X, (int)v.Y, i, a, c, r);
                    //    //mySprites[i].Target = a[y, x];
                    //}
                    //else
                    //{
                    mySprites[i].avoidSprite(mySprites[j]);
                    //}
                    Vector2 v1 = mySprites[i].Target - mySprites[i].Position;
                    float d1 = v1.Length();
                    Vector2 v2 = mySprites[j].Target - mySprites[j].Position;
                    float d2 = v2.Length();
                    if (d1 < Game1.CellWidth  && d2 < Game1.CellWidth)
                    {
                        if (d1 < d2)
                        {
                            //mySprites[i].Target = mySprites[i].Position;
                            Vector2 v = Game1.LocalCell(mySprites[j].Target);
                            othercell((int)v.X, (int)v.Y, j, a, c, r);
                        }
                        else
                        {
                            //mySprites[j].Target = mySprites[j].Position;
                            Vector2 v = Game1.LocalCell(mySprites[i].Target);
                            othercell((int)v.X, (int)v.Y, i, a, c, r);
                        }
                    }
                //}
            }
            else
            {
                if (mySprites[i].TypeMySprite < 6)
                {
                    //vr = new Vector2(r.Next(-10 + i, 10 + i), r.Next(-10 + i, 10 + i));
                    //mySprites[i].Position = mySprites[i].Position + vr;
                    //Nếu mySprites[i] đang đứng im và mySprites[j] đứng im
                    if (mySprites[j].DistanceGo == new Vector2(0))//và đối phương cũng đang đứng
                    {
                        mySprites[i].avoidSpriteStatic(mySprites[j]);
                    }
                    else
                    {
                        mySprites[j].avoidSprite(mySprites[i]);
                    }
                }
                else
                {
                    mySprites[j].avoidSprite(mySprites[i]);

                    Vector2 v1 = mySprites[j].Target - mySprites[j].Position;
                    float d1 = v1.Length();
                    if (d1 < Game1.CellWidth * 2.5f)
                    {
                        Vector2 v = Game1.LocalCell(mySprites[j].Position);
                        othercell((int)v.X, (int)v.Y, j, a, c, r);
                    }
                }
            }
        }

        private void othercell(int x, int y, int i, Vector2[,] a, int c, int r)
        {
            Random rad = new Random();
            if (x > 0 && x < c-1)
                x += rad.Next(-1, 2);
            if (y > 0 && y < r-1)
                y += rad.Next(-1, 2);
            mySprites[i].Target = a[y, x];
        }



        public void Init(ContentManager content, string strPicture, int npic, ref Vector2 topleft, ref Vector2 size, ref int delay, ref int mxblood, ref int armor, ref int damage, int flag, int numState, int type)
        {
            NumberofStates = numState;
            Damage = damage;
            Armor = armor;
            Size = size;
            Maxblood = mxblood;
            Topleft = topleft;
            Texture2D[] texture = new Texture2D[npic];
            for (int i = 0; i < npic; i++)
            {
                texture[i] = content.Load<Texture2D>(strPicture + (i + 1).ToString());
            }
            Texture2D bloodremain = content.Load<Texture2D>("images//others//blood");
            mySprites = new List<MySprite>();
            SpriteModel = new MySprite(texture, topleft, size, delay, armor, damage, flag, bloodremain, Maxblood, NumberofStates, type);
            nSprites = 0;
        }


        public List<MySprite> AutoRangeInit;
        override public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            AutoRangeInit.RemoveRange(0, AutoRangeInit.Count);
            for (int i = 0; i < nSprites; i++)
            {
                AutoRange(mySprites[i]);
            }
            for (int i = 0; i < nSprites; i++)
            {
                Rectangle rect = new Rectangle((int)AutoRangeInit[i].Position.X, (int)AutoRangeInit[i].Position.Y, (int)AutoRangeInit[i].Size.X, (int)AutoRangeInit[i].Size.Y);

                int x = (int)AutoRangeInit[i].Position.X;
                int y = (int)(AutoRangeInit[i].Position.Y - (0.4 * ((int)AutoRangeInit[i].Size.Y)));
                int w = (int)(AutoRangeInit[i].Size.X * (((float)AutoRangeInit[i].Blood) / AutoRangeInit[i].MaxBlood));
                int mw = (int)(AutoRangeInit[i].Size.X);
                Rectangle rectBlood = new Rectangle(x, y, w, 3);
                Rectangle rectMaxBlood = new Rectangle(x, y, mw, 3);
                AutoRangeInit[i].Draw(gameTime, spriteBatch, rect, rectBlood, rectMaxBlood);
            }
        }

        public void AutoRange(MySprite mr)
        {
            if (AutoRangeInit.Count == 0)
            {
                AutoRangeInit.Add(mr);
            }
            else
            {
                for (int i = 0; i < AutoRangeInit.Count; i++)
                {
                    if (AutoRangeInit[i].Position.Y > mr.Position.Y)
                    {
                        int Initremain = AutoRangeInit.Count - i;
                        List<MySprite> list = new List<MySprite>();
                        for (int k = i; k < AutoRangeInit.Count; k++)
                        {
                            list.Add(AutoRangeInit[k]);
                        }
                        AutoRangeInit.RemoveRange(i, Initremain);
                        AutoRangeInit.Add(mr);
                        for (int j = 0; j < Initremain; j++)
                        {
                            AutoRangeInit.Add(list[j]);
                        }
                        return;
                    }
                }
                AutoRangeInit.Add(mr);
            }
        }

        public void SapXepObject()
        {
            //for (int i = 0; i < nSprites; i++)
            //{
            //    if (!mySprites[i].Life || mySprites[i] == null)
            //    {
            //        for (int j = i; j < nSprites - 1; j++)
            //        {
            //            mySprites[j] = mySprites[j + 1];
            //        }
            //        //mySprites[nSprites - 1] = null;
            //        mySprites.RemoveAt(nSprites - 1);
            //        nSprites--;
            //        i--;
            //    }
            //}
            for (int i = 0; i < nSprites; i++)
            {
                if (!mySprites[i].Life || mySprites[i] == null)
                {
                    mySprites.RemoveAt(i);
                    nSprites--;
                    i--;
                }
            }
        }
        int count = 0;
        public void Attack()
        {
            count++;
            for (int i = 0; i < nSprites; i++)
            {
                if (mySprites[i].Life)
                {
                    if (count % 50 == 0)//Nếu Sprite level 10 thì tự phục hồi máu
                    {
                        int lv = mySprites[i].Level;
                        switch (lv)
                        {
                            case 6: mySprites[i].Blood += 1; break;
                            case 7: mySprites[i].Blood += 2; break;
                            case 8: mySprites[i].Blood += 3; break;
                            case 9: mySprites[i].Blood += 4; break;
                            case 10: mySprites[i].Blood += 5; break;
                            default: break;
                        }
                    }
                    bool flagAttack = false;
                    MySprite MSmin = null;
                    int dmin = 0;

                    dmin = 10000;//Dis_Soldier_Deadman(soldier.mySprites[i].PosCenter, Deadman.mySprites[0].PosCenter);
                    MSmin = null;// Deadman.mySprites[0];
                    
                    for (int j = 0; j < nSprites; j++)
                    {
                        if (Game1.Dis_Soldier_Deadman(mySprites[i].PosCenter, mySprites[j].PosCenter) < dmin && mySprites[j].Life && mySprites[j].Flag != mySprites[i].Flag)
                        {
                            dmin = Game1.Dis_Soldier_Deadman(mySprites[i].PosCenter, mySprites[j].PosCenter);
                            MSmin = mySprites[j];
                        }
                        //Nếu đây là con Magic (bơm máu)
                        if (mySprites[i].TypeMySprite == 3 && Game1.Dis_Soldier_Deadman(mySprites[i].PosCenter, mySprites[j].PosCenter) < (Game1.CellWidth * 2.5) && mySprites[j].Flag == 1 && count%50==0 && mySprites[j].TypeMySprite < 6)
                        {
                            mySprites[j].Blood++;
                        }
                    }
                    int d = (int)(Game1.CellWidth * 3.5);
                    if (MSmin != null)
                    {
                        int d1 = d;
                        if (MSmin.TypeMySprite > 5)
                            d1 += Game1.CellWidth;
                        if (dmin < d1 && MSmin.Life)
                        {
                            mySprites[i].BeginAttack(MSmin);
                            flagAttack = true;
                        }
                        else
                        {
                            mySprites[i].FinishAttack();
                        }
                    }
                    else
                    {
                        mySprites[i].FinishAttack();
                    }
                    if (mySprites[i].BeAttacker != null)
                    {
                        if (mySprites[i].BeAttacker.TypeMySprite > 5)
                            d += Game1.CellWidth;
                        if (MSmin != mySprites[i].BeAttacker &&
                            Game1.Dis_Soldier_Deadman(mySprites[i].PosCenter, mySprites[i].BeAttacker.PosCenter) < d)
                        {
                            mySprites[i].BeginAttack(mySprites[i].BeAttacker);
                            flagAttack = true;
                        }
                    }
                    if (!flagAttack)
                    {
                        mySprites[i].FinishAttack();
                    }
                    
                }
            }
        }
    }
}
